package ttOrder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ttOrder.service.IDinnertableService;
import ttOrder.service.IFoodService;
import ttOrder.service.IOrderService;
import ttOrder.vo.CommonMessageResultVO;
import ttOrder.vo.FoodAndType;
import ttOrder.vo.FoodVO;

@Controller
@ResponseBody
public class FoodController {

	@Autowired
	public IFoodService foodService;
	
	@Autowired
	public IDinnertableService dinnertableService;
	
	@Autowired
	public IOrderService orderService;
	
	//查询全部
	@RequestMapping("/showAllFood")
	public ModelAndView showAllFood(ModelAndView mv){
		mv.addObject("foods",foodService.findAllFood());
		mv.setViewName("adminFood/showAllFood");
		return mv;
	}
	
	
	//用户查询全部菜品
	@RequestMapping("/userShowAllFood")
	public ModelAndView userShowAllFood(HttpServletRequest request, HttpServletResponse response, ModelAndView mv){
		Integer tableId=Integer.parseInt(request.getParameter("tableId"));
		dinnertableService.editByTableId(tableId);
		orderService.addOrder(tableId);
		mv.addObject("choosetable",dinnertableService.findDinnertableById(tableId));
		mv.addObject("foods",foodService.findAllFood());
		mv.setViewName("UserFood/userShowAllFood");
		return mv;
	}
	
	//查询全部菜系	
	@RequestMapping(value = "/getFoodTypes",method = RequestMethod.POST)
	public FoodAndType getFoodTypes(ModelAndView mv){
		FoodAndType vo = foodService.getFoodTypes();
		return vo;
	}
	
	//新增菜品
	@RequestMapping(value="/saveFood",method = RequestMethod.POST)
	public CommonMessageResultVO saveTest(@Valid@RequestBody FoodVO foodVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		
		try {
			foodService.addFood(foodVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//显示详细菜品	
	@RequestMapping(value="/getFoodById",method = RequestMethod.POST)
	public FoodVO getFoodById(Integer foodId){		
		FoodVO	resultVO = foodService.getFoodById(foodId);
		return resultVO;
	
	}
	
	//修改菜品
	@RequestMapping(value="/editFood",method = RequestMethod.POST)
	public CommonMessageResultVO editFood(@RequestBody FoodVO foodVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			foodService.editFood(foodVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//删除菜品
	@RequestMapping(value="/deleteFood",method=RequestMethod.POST)
	public boolean deleteFood(Integer foodId){	
		System.out.println(foodId);
			foodService.deleteFood(foodId);		
		return true;
	}	
	
	//根据菜系id查询菜品
	@RequestMapping("/searchFoodByFootType")
	public ModelAndView searchFoodByFootType(Integer foodtypeId,HttpServletRequest request, HttpServletResponse response, ModelAndView mv){
		Integer tableId=Integer.parseInt(request.getParameter("tableId"));
		mv.addObject("choosetable",dinnertableService.findDinnertableById(tableId));
		mv.addObject("foods",foodService.searchFoodByFootType(foodtypeId));
		mv.setViewName("UserFood/showByFoodType");
		return mv;
	}
	
}
